# recommendation_history/models.py
from django.db import models
from django.contrib.auth.models import User

from problems.models import Problem


class RecommendationHistory(models.Model):
    # {'AC', 'WA', 'RE', 'CE', 'TLE', 'MLE', 'PE', 'OLE','SKIP'}
    STATUS_CHOICES = [
        ('AC', 'Accepted'),
        ('WA', 'Wrong Answer'),
        ('TLE', 'Time Limit Exceeded'),
        ('SKIP', 'Skipped'),
        ('MLE', 'Memory Limit Exceeded'),
        ('RE', 'Runtime Error'),
        ('PE', 'Presentation Error'),
        ('OLE', 'Output Limit Exceed'),
        ('CE', 'Computation Error'),
    ]

    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="recommendation_history")
    problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES)

    def __str__(self):
        return f"{self.user.username} - {self.problem.problem_number} - {self.status} at {self.timestamp}"

    class Meta:
        ordering = ['-timestamp']  # 默认按时间戳降序排序

    @classmethod
    def create_record(cls, user, problem, timestamp, status):
        if status in dict(cls.STATUS_CHOICES):
            record = cls(user=user, problem=problem, timestamp=timestamp, status=status)
            record.save()
            return record
        else:
            raise ValueError("Invalid status value")  # 抛出异常
